package com.wavehi.service.impl;

import com.wavehi.mapper.IUserMapper;
import com.wavehi.pojo.User;
import com.wavehi.service.IUserService;
import com.wavehi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author WaveHi
 * @Date 23/9/2021 上午 10:30
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
public class UserServiceImpl implements IUserService {
    @Override
    public Boolean addUser(User user) {
        SqlSession session = null;
        Boolean isCompleted = false;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //添加
            isCompleted = mapper.insert(user);
            return isCompleted;
        } catch (Exception e) {
            e.printStackTrace();
            //异常回滚
            if(session != null) {
                session.rollback();
                session.close();
                session = null;
            }
        } finally {
            //资源的关闭
            if(session != null) {
                session.commit();
                session.close();
                session = null;
            }
            return isCompleted;
        }
    }

    @Override
    public Boolean deleteUser(Long id) {
        SqlSession session = null;
        Boolean isCompleted = false;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //删除
            isCompleted = mapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //异常回滚
            if(session != null) {
                session.rollback();
                session.close();
                session = null;
            }
        } finally {
            //资源的关闭
            if(session != null) {
                session.commit();
                session.close();
                session = null;
            }
            return isCompleted;
        }
    }

    @Override
    public Boolean updateUser(User user) {
        SqlSession session = null;
        boolean isCompleted = false;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //更新
            isCompleted = mapper.update(user);
        } catch (Exception e) {
            e.printStackTrace();
            //异常回滚
            if(session != null) {
                session.rollback();
                session.close();
                session = null;
            }
        } finally {
            //资源的关闭
            if(session != null) {
                session.commit();
                session.close();
                session = null;
            }
            return isCompleted;
        }
    }

    @Override
    public Boolean userExist(String username) {
        SqlSession session = null;
        boolean exists = false;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //验证
            User user = mapper.selectByUsername(username);
            if(user != null) {
                exists = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return exists;
        }
    }

    @Override
    public Boolean userExist(String username, String password) {
        SqlSession session = null;
        boolean exists = false;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询指定用户
            User user = mapper.selectByUsernameAndPassword(username, password);
            //验证信息
            if(user != null) {
                exists = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return exists;
        }
    }

    @Override
    public User getUser(Long id) {
        SqlSession session = null;
        User user = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询用户
            user = mapper.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return user;
        }
    }

    @Override
    public User getUser(String username) {
        SqlSession session = null;
        User user = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            user = mapper.selectByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return user;
        }
    }

    @Override
    public List<User> getAllUser() {
        SqlSession session = null;
        List<User> users = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            users = mapper.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return users;
        }
    }

    @Override
    public List<User> getPartOfUser(int page, int size) {
        SqlSession session = null;
        List<User> users = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            int offset = (page - 1) * size;
            users = mapper.selectByOffsetAndSize(offset, size);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return users;
        }
    }
}
